﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using BEGDC.CC.BL.Common;
using BEGDC.CC.BL.Workflow;
using BEGDC.CC.Entity;
using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web
{
    /// <summary>
    /// Summary description for ShowTypeForm.
    /// </summary>
    public class ShowCostCenterCofigurationForm : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label lbName;
        protected System.Web.UI.WebControls.DropDownList ddlCostCenter;
        protected System.Web.UI.WebControls.DataGrid dgUserDept;
        protected System.Web.UI.WebControls.Button btnSave;
        protected System.Web.UI.WebControls.Button btnClose;
        private int UserRoleID;

        private void Page_Load(object sender, System.EventArgs e)
        {
            this.UserRoleID = Convert.ToInt32(Request.QueryString["id"]);

            // Put user code to initialize the page here
            if (!Page.IsPostBack)
            {
                InitializeGlobalization();
                BindDept();
                BindUserRole(Convert.ToInt32(UserRoleID));

                if (((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles.IndexOf("BusinessAdmin") > -1 && ((BEGDC.CC.BL.Workflow.CCPrincipal)Page.User).Roles.IndexOf("RoleAdmin") == -1)
                {
                    this.btnSave.Visible = false;
                }

            }
        }

        private void BindDept()
        {
            //			BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
            //			BEGDC.CC.Entity.UserRole userrole = iuser.GetUserRoleByID(id);

            IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
            IList listCostCenter = utility.BuildDicList(BEGDC.CC.BL.Common.DicType.CostCenter);

            this.ddlCostCenter.Items.Add(new System.Web.UI.WebControls.ListItem(string.Empty, "0"));

            foreach (BEGDC.CC.Entity.DicItem item in listCostCenter)
            {
                this.ddlCostCenter.Items.Add(new System.Web.UI.WebControls.ListItem(item.DicItemKey, item.Id.ToString()));
            }

            //			if (userrole.CATBranch != null)
            //				this.ddlCostCenter.SelectedValue = userrole.CATBranch.Id.ToString();
        }

        private void BindUserRole(int id)
        {
            BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
            DataTable dt = iuser.GetCostCenterHeadByUserRoleID(id);
            this.dgUserDept.DataSource = dt.DefaultView;
            this.dgUserDept.DataBind();
        }

        private void InitializeGlobalization()
        {
            this.lbName.Text = "Cost Center: ";
        }
        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);
            this.dgUserDept.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgUserDept_ItemDataBound);
            this.dgUserDept.ItemCommand += new DataGridCommandEventHandler(dgUserDept_ItemCommand);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion


        void btnSave_Click(object sender, EventArgs e)
        {
            BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;

            int centerid = Convert.ToInt32(this.ddlCostCenter.SelectedValue);
            foreach (DataGridItem di in this.dgUserDept.Items)
            {
                if (di.Cells[1].Text == centerid.ToString())
                {
                    Page.RegisterClientScriptBlock("", "<script language='javascript'>alert('This CostCenter is alreay assigned!');</script>");
                    return;
                }
            }


            if (iuser.SaveCostCenterHead(this.UserRoleID, centerid))
            {
                IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                this.BindUserRole(this.UserRoleID);
                CCIdentity id = this.User.Identity as CCIdentity;
                BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                log.Module = "CostCenter Head Configuration";
                Employee emp = new Employee();
                emp.Id = id.UserInfo.Id;
                log.Employee = emp;
                log.Time = System.DateTime.Now;
                log.Description = "Update CostCenter Head property of Employee .";
                utility.SaveOperationLog(log);
            }
            else
                Page.RegisterClientScriptBlock("", "<script>alert('" + BEGDC.CC.Resource.SystemMessage.CR002 + "');</script>");

        }

        void btnClose_Click(object sender, EventArgs e)
        {
            Page.RegisterClientScriptBlock("", "<script>returnValue='';window.close();</script>");
        }

        private void dgUserDept_ItemDataBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                string userroleid = e.Item.Cells[0].Text;
                ImageButton iblegal = (ImageButton)e.Item.FindControl("imageLegal");
                iblegal.Attributes.Add("onclick", "javascript:OnDelete();");
            }
        }

        private void dgUserDept_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                BEGDC.CC.BL.UserManagement.IUser iuser = AppContainer.Instance[ServiceConst.UserService] as BEGDC.CC.BL.UserManagement.IUser;
                if (iuser.RemoveCostCenterHead(Convert.ToInt32(e.Item.Cells[0].Text), Convert.ToInt32(e.Item.Cells[1].Text)))
                {
                    this.BindUserRole(this.UserRoleID);
                    //write log
                    IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
                    CCIdentity id = this.User.Identity as CCIdentity;
                    BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
                    log.Module = "CostCenter Head Configuration";
                    Employee emp = new Employee();
                    emp.Id = id.UserInfo.Id;
                    log.Employee = emp;
                    log.Time = System.DateTime.Now;
                    log.Description = "Delete CostCenter Head property of Employee " + e.Item.Cells[3].Text;
                    utility.SaveOperationLog(log);
                }
            }
        }
    }
}
